package com.achun.getcouponleague.presenter.impl;

import com.achun.getcouponleague.Bean.SelectCategories;
import com.achun.getcouponleague.Bean.SelectContent;
import com.achun.getcouponleague.model.ServiceApi;
import com.achun.getcouponleague.presenter.ISelectorPresenter;
import com.achun.getcouponleague.utils.Logutils;
import com.achun.getcouponleague.utils.RetrogitManager;
import com.achun.getcouponleague.view.ISelectorCallBack;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SelectorPresenter implements ISelectorPresenter {
    private ISelectorCallBack iSelectorCallBack;
    @Override
    public void registerCallback(ISelectorCallBack callback) {
    this.iSelectorCallBack=callback;
    }

    @Override
    public void unRegisterCallback(ISelectorCallBack callback) {
    this.iSelectorCallBack=null;
    }

    @Override
    public void getSelectCategories() {
        if (iSelectorCallBack != null) {
            iSelectorCallBack.onLoading();
        }
        Retrofit retrofit = RetrogitManager.getInstance().getRetrofit();
        ServiceApi serviceApi = retrofit.create(ServiceApi.class);
        Call<SelectCategories> task = serviceApi.getSelectCategories();
        task.enqueue(new Callback<SelectCategories>() {
            @Override
            public void onResponse(Call<SelectCategories> call, Response<SelectCategories> response) {
                int code = response.code();
                if (code== HttpURLConnection.HTTP_OK){
                    SelectCategories selectCategories = response.body();
                    //成功
                    handleSuccess(selectCategories);
                }else {
                    //错误处理
                    handleOnError();
                }
            }

            @Override
            public void onFailure(Call<SelectCategories> call, Throwable t) {
//错误处理
                handleOnError();
            }
        });

    }

    private void handleOnError() {
        if (iSelectorCallBack != null) {
            iSelectorCallBack.onError();
        }
    }

    private void handleSuccess(SelectCategories selectCategories) {
        if (iSelectorCallBack != null) {
            if (selectCategories.getData().size()==0){
                iSelectorCallBack.onEmpty();
            }else {
                iSelectorCallBack.onLoadedCategorys(selectCategories);
            }
        }
    }

    @Override
    public void getContentById(int id) {
        Logutils.e(this.getClass(),"categoryid-->"+id);
        Retrofit retrofit = RetrogitManager.getInstance().getRetrofit();
        ServiceApi serviceApi = retrofit.create(ServiceApi.class);
        Call<SelectContent> task = serviceApi.getContentById(id);
        task.enqueue(new Callback<SelectContent>() {
            @Override
            public void onResponse(Call<SelectContent> call, Response<SelectContent> response) {
                int code = response.code();
                if (code==HttpURLConnection.HTTP_OK){
                    SelectContent selectContent = response.body();
                    if (iSelectorCallBack != null) {
                        iSelectorCallBack.onLoadedContentById(selectContent);
                    }
                }else {
                    //网络错误
                    handleOnError();
                }
            }

            @Override
            public void onFailure(Call<SelectContent> call, Throwable t) {
//网络错误
                Logutils.e(this.getClass(),"加载分类内容错误---》"+t.toString());
                handleOnError();
            }
        });
    }
}
